<template>
  <div class="member-list">
    <div class="statistical">
      <div class="title">
        考勤统计
      </div>
      <div class="statistical-box">
        <div class="statistical-box-item">
          <span class="name">{{meetingInfo.meetingStatus === 5 ? "已签退" : "已签到"}}</span>
          <span class="count">{{alreadySignCount}}</span>
          <span class="percentage">{{alreadySignCountPct}}%</span>
        </div>
        <div class="statistical-box-item">
          <span class="name">{{meetingInfo.meetingStatus === 5 ? "未签退" : "未签到"}}</span>
          <span class="count">{{notSignCount}}</span>
          <span class="percentage">{{notSignCountPct}}%</span>
        </div>
        <div class="statistical-box-item">
          <span class="name">迟到</span>
          <span class="count">{{lateCount}}</span>
          <span class="percentage">{{lateCountPct}}%</span>
        </div>
      </div>
    </div>
    <div class="member-list-box">
      <div class="title">
        <span>参会人员</span>
        <span @click="replayHandle" class="replay"><van-icon name="replay" /></span>
      </div>
      <div class="content" @touchmove="touchmoveHandle">
        <div class="item" v-for="(item, index) in MeetingNoticeData" :key="index" @click="memberSelect(item)">
          <span class="head iconfont icon-touxiang3"></span>
          <span class="name">{{ item.userName }}</span>
          <span class="status">{{ item.isLeave === 1 ? "已请假" : signInStatus[item.signInStatus] }}</span>
          <span
            class="operation iconfont icon-gengduo"></span
          >
        </div>
      </div>
    </div>
    <van-overlay :show="overlayShow" @click="overlayShow = false">
      <div class="wrapper">
        <div class="block" @click.stop>
          <div class="close"><van-icon @click="overlayShow = false" name="cross" style="margin-right: 0.8rem;" size="1.5rem" /></div>
          <van-cell-group>
            <van-cell title="姓名" :value="userName" />
            <van-cell title="部门" :value="departmentNumber" />
            <van-cell title="性别" :value="sexList[sex]" />
            <van-cell title="手机号" :value="phone ? phone : '未填写'" />
            <van-cell title="签到方式" :value="signMethod[signInMethod]" v-if="isignInStatus !== 0" />
            <van-cell title="签到时间" :value="signInTime" v-if="isignInStatus !== 0" />
            <van-cell title="签退方式" :value="signMethod[signOutMethod]" v-if="isignOutStatus !== 0" />
            <van-cell title="签退时间" :value="signOutTime" v-if="isignOutStatus !== 0" />
            <div class="btn-box" v-if="isignInStatus === 0 && number === meetingInfo.userNumber" @click="goRetroactive(userNumber)">
              <div class="btn">
                <span>手动签到</span>
              </div>
            </div>
            <!-- <div class="item">
              <div class="label">
                <span>操作</span>
              </div>
              <div class="value">
                <van-button type="primary" size="mini">补签</van-button>
              </div>
            </div> -->
            <!-- <van-cell title="操作" value="补签" label="点击状态进行补签" /> -->
          </van-cell-group>
        </div>
      </div>
    </van-overlay>
  </div>
</template>

<script>
import { toRefs, ref, reactive ,computed, watch } from 'vue'
import { manualSign } from '@/api/meeting/meeting.js'
import { useStore } from 'vuex'
import { signInStatus } from '@/enum.js'
import { getUserInfoByUserNum } from "@/api/my/user.js"
import { Notify } from 'vant'
import { sexList, signMethod } from '@/enum.js'
export default {
  name: "MemberList",
  props: {
    meetingId: {
      type: String,
      required: true,
      default: "",
    },
    meetingInfo: {
      type: Object,
      required: true
    },
    MeetingNoticeData: {
      type: Array,
      required: true
    }
  },
  emits: ["goAgainGetNotice", "replayHandle"],
  setup(props, context) {
    const store = useStore()

    const number = computed(() => store.getters.getUserNumber)

    const { meetingId, meetingInfo, MeetingNoticeData } = toRefs(props)

    let alreadySignCount = ref(0) 
    let notSignCount = ref(0) 
    let lateCount = ref(0)

    let alreadySignCountPct = ref(0) 
    let notSignCountPct = ref(0) 
    let lateCountPct = ref(0)

    let statusEnum = {
      1: "signInStatus",
      3: "signInStatus",
      4: "signInStatus",
      5: "signOutStatus"
    }

    // 考勤统计
    function statisticalAttendance() {
      if (!meetingInfo.value.meetingStatus) return
      if (!MeetingNoticeData.value) return

      // 统计请假人员人数
      let leaveCount = 0

      MeetingNoticeData.value.forEach(t => {
        if (t.isLeave === 1) leaveCount ++
      })

      notSignCount.value = 0
      alreadySignCount.value = 0
      lateCount.value = 0

      const total = MeetingNoticeData.value.length - leaveCount
   
      MeetingNoticeData.value.forEach(t => {
        if (t.isLeave === 1) return
        switch(t[statusEnum[meetingInfo.value.meetingStatus]]) {
          case 0: 
            notSignCount.value ++;
            break;
          case 1: 
            alreadySignCount.value ++;
            break;
          case 2: 
            lateCount.value ++;
            break;
        }
      })

      alreadySignCountPct.value = parseInt(alreadySignCount.value / total * 100)
      notSignCountPct.value = parseInt(notSignCount.value / total * 100)
      lateCountPct.value = parseInt(lateCount.value / total * 100)
    }

    watch(() =>  meetingInfo.value, () => {
      statisticalAttendance()
    })

    watch(() => MeetingNoticeData.value, () => {
      statisticalAttendance()
    }) 

    //遮罩层展显示变量
    let overlayShow = ref(false)

    let userInfo = reactive({
      userName: "",
      userNumber: "",
      phone: "",
      userType: 0,
      sex: 0,
      departmentNumber: "",
      isignInStatus: 0,
      isignOutStatus: 0,
      signInMethod: 0,
      signInTime: '',
      signOutMethod: 0,
      signOutTime: '',
    })

    // 点击成员处理函数
    async function memberSelect(item) {
      overlayShow.value = true
      console.log(item)
      const res = await getUserInfoByUserNum({number: item.userNumber})
      if (res.data.code === 400) {
        Notify({ type: 'warning', duration: 1000, message: res.data.message })
        return
      }
      console.log(res)
      userInfo.userName = res.data.data.name
      userInfo.userNumber = res.data.data.number
      userInfo.phone = res.data.data.phone
      userInfo.userType = res.data.data.type
      userInfo.sex = res.data.data.sex
      userInfo.departmentNumber = res.data.data.departmentNumber
      userInfo.isignInStatus = item.signInStatus
      userInfo.isignOutStatus = item.signOutStatus
      userInfo.signInMethod = item.signInMethod
      userInfo.signInTime = item.signInTime ? item.signInTime.split(" ")[1] : ""
      userInfo.signOutMethod = item.signOutMethod
      userInfo.signOutTime = item.signOutTime ? item.signOutTime.split(" ")[1] : ""
    } 

    // 补签处理函数
    async function goRetroactive(number) {
      if (meetingInfo.value.meetingStatus === 1) {
        Notify({ type: 'warning', duration: 1000, message: "会议还未开始！"})
        return
      }
      console.log(number)
      overlayShow.value = false
      const res = await manualSign({
        meetingId: meetingId.value,
        userNumber: number,
      })
      console.log(res)
      if (res.data.code === 400) {
        Notify({ type: 'warning', duration: 1000, message: res.data.message })
        return
      }
      // 重新获取考勤列表
      context.emit("goAgainGetNotice")
      Notify({ type: 'success', duration: 1000, message: "补签成功！" })
    }

    // 刷新
    function replayHandle() {
      context.emit("replayHandle")
    }

    return {
      ...toRefs(userInfo),
      overlayShow,
      number,
      alreadySignCount,
      notSignCount,
      lateCount,
      alreadySignCountPct,
      notSignCountPct,
      lateCountPct,
      memberSelect,
      signInStatus,
      goRetroactive,
      signMethod,
      sexList,
      replayHandle
    }
  }
}
</script>

<style lang="scss">
.member-list {
  width: 100vw;
  height: calc(100% - 6rem);
  padding: 0 1rem;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;

  .title {
    width: 100%;
    height: 3rem;
    box-sizing: border-box;
    padding: 0 1rem;
    line-height: 3rem;
    font-size: 1.4rem;
    font-weight: bold;
    border-bottom: 0.1rem solid var(--gray-color3);
    display: flex;
    align-items: center;
    justify-content: space-between;

    span {
      font-size: 1.4rem;
      font-weight: bold;

      &:last-child {
        color: var(--gray-color2);
        font-weight: normal;
      }
    }
  }

  .statistical {
    width: 100%;
    height: 12rem;
    border-radius: var(--global-border-radius);
    background-color: #FFFFFF;
    margin-top: 1rem;
    box-shadow: var(--box-shadow);
    // box-sizing: border-box;
    // padding: 0 1rem;

    .title {
      padding: 0 2rem;
    }

    &-box {
      height: calc(100% - 3rem);
      font-size: 1.3rem;
      display: flex;

      &-item {
        height: 100%;
        flex: 1;
        display: flex;
        flex-direction: column;
        border-right: 0.1rem solid var(--gray-color3);
        align-items: center;
        justify-content: center;

        &:last-child {
          border: none;
        }

        .name {
          font-size: 1.2rem;
          font-weight: 500;
        }

        .count {
          font-size: 2.5rem;
          font-weight: bold;
          margin-top: 0.5rem;
        }

        .percentage {
          margin-top: 1rem;
          font-size: 1.2rem;
          color: var(--blue-color1);
        }
      }
    }
  }
  

  &-box {
    flex: 1;
    overflow: hidden;
    background-color: #FFFFFF;
    box-sizing: border-box;
    margin-top: 1rem;
    border-radius: var(--global-border-radius);
    padding: 0 1rem;
    padding-bottom: 1rem;
    box-shadow: var(--box-shadow);

    .title {
      position: sticky;
      left: 0;
      top: 0;
      background-color: #FFFFFF;

      .replay {
        transition: all 0.2s;
        &:active {
          transform: rotate(360deg);
        }
      }
    }

    .content {
      width: 100%;
      overflow-x: hidden;
      overflow-y: scroll;
      height: calc(100% - 3rem);

      &::-webkit-scrollbar {
        display: none;
      }

      .item {
        width: 100%;
        height: 4rem;
        // background-color: #666;
        font-size: 1.4rem;
        border-bottom: 1px solid var(--gray-color3);
        box-sizing: border-box;
        padding: 0 1.5rem;
        display: flex;
        align-items: center;

        &:active {
          background-color: var(--gray-color4);
        }

        .name {
          flex: 1;
          margin-left: 1rem;
          line-height: 4rem;
        }
        .head {
          font-size: 2rem;
          // color: var(--second-text-color);
        }
        .status {
          font-size: 1.2rem;
          color: var(--second-text-color);
          margin-right: 1rem;
        }
        .operation {
          font-size: 1.2rem;
          color: var(--second-text-color);
        }
      }
    }
  }

  .wrapper {
          display: flex;
          align-items: center;
          justify-content: center;
          height: 100%;
          
          .block {
            width: 30rem;
            min-height: 20rem;
            border-radius: 1rem;
            box-sizing: border-box;
            padding: 1rem;
            background-color: #fff;
            display: flex;
            flex-direction: column;

            .close {
              width: 100%;
              height: 2rem;
              margin: 0.5rem 0;
              display: flex;
              justify-content: right;
            }

            .btn-box {
              width: 100%;
              height: 4rem;
              display: flex;
              justify-content: center;
              align-items: center;
              .btn {
                // background: var(--btn-background);
                width: 100%;
                height: 100%;
                border-radius: 1rem;
                text-align: center;
                line-height: 4rem;
                color: #000;
                font-size: 1.4rem;
                // span {
                //   font-weight: 500;
                // }
              }
            }
            

            .item {
              width: 100%;
              display: flex;
              // justify-content: space-between;
              box-sizing: border-box;
              color: var(--van-cell-text-color);
              font-size: var(--van-cell-font-size);
              padding: var(--van-cell-vertical-padding) var(--van-cell-horizontal-padding);
              line-height: var(--van-cell-line-height);

              .label {
                flex: 1;
              }
            }
          }
    }

    .member-item {
      width: 100%;
      height: 4rem;
      // background-color: #666;
      font-size: 1.5rem;
      border-bottom: 1px solid #eee;
      box-sizing: border-box;
      padding: 0 1.5rem;
      display: flex;
      align-items: center;
      &:active {
        background-color: var(--gray-color4);
      }
      .name {
        flex: 1;
        margin-left: 1rem;
        line-height: 4rem;
      }
      .head {
        font-size: 2rem;
        // color: var(--second-text-color);
      }
      .status {
        font-size: 1.3rem;
        color: var(--second-text-color);
        margin-right: 1rem;
      }
      .operation {
        font-size: 1.3rem;
        color: var(--second-text-color);
      }
    }
}
</style>